import 'dart:io';

import 'package:flutter_animation/utils/constant.dart';
import 'package:test/test.dart';
import 'package:flutter_driver/flutter_driver.dart';

void main() {
  group('Test App', () {

    late FlutterDriver driver;

    // 测试开始前链接FlutterDriver
    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    // 测试结束后关闭FlutterDriver
    tearDownAll(() async {
      await driver.close();
    });

    test('登录', () async{
      final phoneTF = find.byValueKey(Constant.loginPhoneTFKey);
      final pswTF = find.byValueKey(Constant.loginPswTFKey);
      final loginBtn = find.byValueKey(Constant.loginButtonKey);

      await driver.tap(phoneTF);
      await driver.enterText('13580808080');
      await Future.delayed(Duration(seconds: 2));

      await driver.tap(pswTF);
      await driver.enterText('123456');
      await Future.delayed(Duration(seconds: 2));

      await driver.tap(loginBtn);

    });

    test('切换tabbar', () async{
      // 定位元素
      final firstNavigationBarWidget = find.byTooltip(Constant.firstNavigationBarItemToolTip);
      final secondNavigationBarWidget = find.byTooltip(Constant.secondNavigationBarItemToolTip);
      final thirdNavigationBarWidget = find.byTooltip(Constant.thirdNavigationBarItemToolTip);
      await Future.delayed(Duration(seconds: 2));
      await driver.tap(secondNavigationBarWidget);
      await Future.delayed(Duration(seconds: 2));
      await driver.tap(thirdNavigationBarWidget);
      await Future.delayed(Duration(seconds: 2));
      await driver.tap(firstNavigationBarWidget);
    });
    test('跳转页面', () async {
      await Future.delayed(Duration(seconds: 2));
      await driver.tap(find.byValueKey(Constant.homeJumpListKey));
      expect(await driver.getText(find.byValueKey(Constant.listTitleKey)), Constant.listTitle);
    });
    test('滑动页面到底部', () async {
      await driver.runUnsynchronized(() async {
        final listFinder = find.byValueKey(Constant.listKey);
        final itemFinder = find.byValueKey('item_66');
        await driver.scrollUntilVisible(
          listFinder,
          itemFinder,
          dyScroll: -400.0,
        );
      });
    });
    test('返回页面', () async {
      await Future.delayed(Duration(seconds: 5));
      final buttonFinder = find.byValueKey(Constant.listBackKey);
      await driver.tap(buttonFinder);
      await Future.delayed(Duration(seconds: 2));
    });

  });
}